package com.asm.controller.api;

import com.asm.controller.BaseController;
import com.asm.dto.PageResult;
import com.asm.dto.Result;
import com.asm.service.CommunityService;
import com.asm.service.DistrictService;
import com.asm.service.SpotService;
import com.asm.service.TagService;
import com.asm.vo.sys.CommunityVo;
import com.asm.vo.sys.SpotVo;
import com.asm.vo.sys.TagVo;
import io.swagger.annotations.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 小区页面接口
 * @author morningbear
 */
@Slf4j
@RestController
@RequestMapping("/communityManage")
@ApiModel("用于处理小区信息的Controller层接口")
public class CommunityController extends BaseController {
    @Resource
    private CommunityService communityService;

    @Resource
    private SpotService spotService;

    @Resource
    private TagService tagService;

    /**
     * 查询所有小区
     *
     * @param pageNumber 页数
     * @param pageSize   每页大小
     * @return ResponseEntity<PageResult<CommunityVo>>
     */
    @ApiOperation("查询所有小区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "页大小")
    })
    @GetMapping("/communities")
    public ResponseEntity<PageResult<CommunityVo>> getAllCommunities(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize){
        return new ResponseEntity(new Result().ok("success", communityService.findAllCommunities(pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据小区类型查找小区
     *
     * @param pageNumber 页数
     * @param pageSize   每页大小
     * @param type       小区类型
     * @return ResponseEntity<PageResult<CommunityVo>>
     */
    @ApiOperation("根据小区类型查找小区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "页大小"),
            @ApiImplicitParam(name = "type", value = "小区类型")
    })
    @GetMapping("/communities/type/{type}")
    public ResponseEntity<PageResult<CommunityVo>> getCommunityByType(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize, @PathVariable String type){
        return new ResponseEntity(new Result().ok("success", communityService.findByCommunityType(type, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据小区名字查找小区
     *
     * @param pageNumber 页码
     * @param pageSize 页大小
     * @param name 小区名字
     * @return ResponseEntity<PageResult<CommunityVo>>
     */
    @ApiOperation("根据小区名称查找小区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "页大小"),
            @ApiImplicitParam(name = "name", value = "小区类型")
    })
    @GetMapping("/communities/name/{name}")
    public ResponseEntity<PageResult<CommunityVo>> getCommunityByName(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize, @PathVariable String name){
        return new ResponseEntity(new Result().ok("success", communityService.findByCommunityName(name, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据镇/区编号查找小区
     *
     * @param pageNumber 页码
     * @param pageSize 页大小
     * @param districtId 编号
     * @return HttpStatus
     */
    @ApiOperation("根据小区类型查找小区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "页大小"),
            @ApiImplicitParam(name = "type", value = "小区类型")
    })
    @GetMapping("/communities/districtId/{districtId}")
    public ResponseEntity<PageResult<CommunityVo>> getCommunityByDistrictId(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize, @PathVariable long districtId){
        return new ResponseEntity(new Result().ok("success", communityService.findByCommunityDistrictId(districtId, pageNumber, pageSize)), HttpStatus.OK);

    }

    /**
     * 根据商圈编号查找小区
     *
     * @param pageNumber 页码
     * @param pageSize 页大小
     * @param tradeAreaId 商圈编号
     * @return HttpStatus
     */
    @ApiOperation("根据商圈编号查找小区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "页大小"),
            @ApiImplicitParam(name = "tradeAreaId", value = "商圈编号")
    })
    @GetMapping("/communities/tradeAreaId/{tradeAreaId}")
    public  ResponseEntity<PageResult<CommunityVo>> getCommunityByTradeAreaId(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize, @PathVariable long tradeAreaId){
        return new ResponseEntity(new Result().ok("success",  communityService.findByCommunityTradeAreaId(tradeAreaId, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据编号查询某一个小区
     *
     * @param id 小区编号
     * @return HttpStatus
     */
    @ApiOperation("根据编号查询某一个小区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "小区编号")
    })
    @GetMapping("/community/{id}")
    public ResponseEntity<Result> getCommunity(@PathVariable("id") Long id){
        return new ResponseEntity(new Result().ok("success", communityService.getCommunity(id)), HttpStatus.OK);
    }

    /**
     * 根据省份、城市和地区查询小区信息
     *
     * @param info 封装的省份、城市、地区
     * @return 小区列表
     */
    @ApiOperation("根据省份、城市和地区查询小区信息")
    @PostMapping("/community/getCommunitiesByParams")
    public ResponseEntity<Result> getCommunitiesByParams(@RequestBody @ApiParam(name = "info", value = "封装的省份、城市、地区") Info info) {
        List<CommunityVo> list = communityService.getCommunitiesByParams(info.getProvince(), info.getCity(), info.getDistrict());
        return new ResponseEntity<>(new Result().ok("success", list), HttpStatus.OK);
    }

    /**
     * 小区添加
     *
     * @param communityVo 小区信息
     * @return HttpStatus
     */
    @ApiOperation("小区添加")
    @PostMapping("/community")
    public ResponseEntity<Result> addCommunity(@RequestBody @ApiParam(name = "communityVo", value = "小区信息") CommunityVo communityVo){
        communityService.addCommunity(communityVo);
        return new ResponseEntity(new Result().ok("add success"), HttpStatus.OK);
    }

    /**
     * 小区删除
     *
     * @param id 小区ID
     * @return HttpStatus
     */
    @ApiOperation("小区删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "小区编号")
    })
    @DeleteMapping("/community/{id}")
    public ResponseEntity<Result> deleteCommunity(@PathVariable("id") Long id){
        communityService.deleteCommunity(id);
        return new ResponseEntity(new Result().ok("delete success"), HttpStatus.OK);
    }

    /**
     * 小区修改
     *
     * @param communityVo 小区
     * @return HttpStatus
     */
    @ApiOperation("小区修改")
    @PutMapping("/community")
    public ResponseEntity<Result> updateCommunity(@RequestBody @ApiParam(name = "communityVo", value = "小区信息") CommunityVo communityVo){
        communityService.updateCommunity(communityVo);
        return new ResponseEntity(new Result().ok("register success"), HttpStatus.OK);
    }

    /**
     * 查看小区所属广告位
     *
     * @param communityId 小区ID
     * @return HttpStatus
     */
    @ApiOperation("查看小区所属广告位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "页大小"),
            @ApiImplicitParam(name = "communityId", value = "小区编号")
    })
    @GetMapping("/spots/{communityId}")
    public ResponseEntity<PageResult<SpotVo>> getCommunitySpots(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize, @PathVariable Long communityId){
        return new ResponseEntity(new Result().ok("success", spotService.findSpotByCommunityId(communityId, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 查看标签
     *
     * @param pageNumber 页码
     * @param pageSize 页大小
     * @return httpStatus
     */
    @ApiOperation("查看标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "页大小")
    })
    @GetMapping("/tags")
    public ResponseEntity<PageResult<TagVo>> getAllTags(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber, @RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize){
        return new ResponseEntity(new Result().ok("success", tagService.findAllTags(pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 添加标签
     *
     * @param tagVo 标签信息
     * @return httpStatus
     */
    @ApiOperation("添加标签")
    @PostMapping("/tag")
    public ResponseEntity<Result> addTag(@RequestBody @ApiParam(name = "tagVo", value = "标签信息") TagVo tagVo){
        tagService.addTag(tagVo);
        return new ResponseEntity<>(new Result().ok("add success"), HttpStatus.OK);
    }

    /**
     * 更新标签
     *
     * @param tagVo 标签信息
     * @return httpStatus
     */
    @ApiOperation("更新标签")
    @PutMapping("/tag")
    public ResponseEntity<Result> updateTag(@RequestBody @ApiParam(name = "tagVo", value = "标签信息") TagVo tagVo){
        tagService.updateTag(tagVo);
        return new ResponseEntity<>(new Result().ok("add success"), HttpStatus.OK);
    }

    /**
     * 删除标签
     *
     * @param id 标签id
     * @return httpStatus
     */
    @ApiOperation("更新标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "标签编号")
    })
    @DeleteMapping("/tag/{id}")
    public ResponseEntity<Result> deleteTag(@PathVariable("id") Long id){
        tagService.deleteTag(id);
        return new ResponseEntity<>(new Result().ok("add success"), HttpStatus.OK);
    }

}

@Data
class Info {
    private String province;

    private String city;

    private String district;
}
